using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace CroCommando
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public enum GameState { Start, InGame, Menu, GameOver };
        public GameState currentGameState;

        public enum MenuState { Intro, InGame, GameOver };
        public MenuState currentMenuState;

        BackgroundHandler backgroundHandler;
        InGame inGame;
        MenuHandler menuHandler;
        GameHUD gameHUD;
        AudioEngine audioEngine;
        WaveBank waveBank;
        public SoundBank soundBank;
        Cue trackCue;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferredBackBufferHeight = 768;
            graphics.IsFullScreen = true;
            graphics.ApplyChanges();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            currentGameState = GameState.InGame;
            currentMenuState = MenuState.Intro;

            backgroundHandler = new BackgroundHandler(this);
            inGame = new InGame(this);
            menuHandler = new MenuHandler(this);
            gameHUD = new GameHUD(this);

            Components.Add(backgroundHandler);
            Components.Add(inGame);
            Components.Add(menuHandler);
            Components.Add(gameHUD);

            backgroundHandler.Enabled = false;
            backgroundHandler.Visible = false;
            inGame.Enabled = false;
            inGame.Visible = false;
            menuHandler.Enabled = false;
            menuHandler.Visible = false;
            gameHUD.Enabled = false;
            gameHUD.Visible = false;

            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferredBackBufferHeight = 768;
            graphics.ApplyChanges();

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //audioEngine = new AudioEngine(@"Content\Audio\GameAudio.xgs");
            //waveBank = new WaveBank(audioEngine, @"Content\Audio\Wave Bank.xwb");
            //soundBank = new SoundBank(audioEngine, @"Content\Audio\Sound Bank.xsb");

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            backgroundHandler.Enabled = false;
            backgroundHandler.Visible = false;
            inGame.Enabled = false;
            inGame.Visible = false;
            menuHandler.Enabled = false;
            menuHandler.Visible = false;
            gameHUD.Enabled = false;
            gameHUD.Visible = false;

            switch (currentGameState) { 
                case GameState.Start:
                    menuHandler.CurrentMenuState = MenuState.Intro;
                    menuHandler.Enabled = true;
                    menuHandler.Visible = true;

                    break;
                case GameState.InGame:
                    backgroundHandler.Enabled = true;
                    backgroundHandler.Visible = true;
                    inGame.Enabled = true;
                    inGame.Visible = true;
                    gameHUD.Enabled = true;
                    gameHUD.Visible = true;

                    break;
                case GameState.Menu:
                    menuHandler.CurrentMenuState = MenuState.InGame;
                    menuHandler.Enabled = true;
                    menuHandler.Visible = true;

                    break;
                case GameState.GameOver:
                    menuHandler.CurrentMenuState = MenuState.GameOver;
                    menuHandler.Visible = true;

                    break;
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            backgroundHandler.Enabled = false;
            backgroundHandler.Visible = false;
            inGame.Enabled = false;
            inGame.Visible = false;
            menuHandler.Enabled = false;
            menuHandler.Visible = false;

            switch (currentGameState)
            {
                case GameState.Start:
                    menuHandler.CurrentMenuState = MenuState.Intro;
                    menuHandler.Enabled = true;
                    menuHandler.Visible = true;

                    break;
                case GameState.InGame:
                    backgroundHandler.Enabled = true;
                    backgroundHandler.Visible = true;
                    inGame.Enabled = true;
                    inGame.Visible = true;

                    break;
                case GameState.Menu:
                    menuHandler.CurrentMenuState = MenuState.InGame;
                    menuHandler.Enabled = true;
                    menuHandler.Visible = true;

                    break;
                case GameState.GameOver:
                    menuHandler.CurrentMenuState = MenuState.GameOver;
                    menuHandler.Visible = true;

                    break;
            }

            base.Draw(gameTime);
        }

        public void PlayCue(string cue) {
            soundBank.PlayCue(cue);
        }

        public GameState CurrentGameState
        {
            get;
            set;
        }
    }
}
